// import { emojiMap, account_emoji, account_emojiMap, emojiAllMap } from './emojiMap';

// store统一设置
export const setCache = (key: string, value: any) => {
    if (!key) throw new Error('key is required');
    uni.setStorageSync(`QY_${key?.toUpperCase()}`, value);
};
export const getCache = (key: string) => {
    if (!key) throw new Error('key is required');
    return uni.getStorageSync(`QY_${key?.toUpperCase()}`);
};
export const removeCache = (key: string) => {
    if (!key) throw new Error('key is required');
    uni.removeStorageSync(`QY_${key?.toUpperCase()}`);
};

// 随机ID
export const genId = (prefix?: number | string) =>
    `${prefix || ''}${Math.random().toString(16).slice(2)}`;

// 将数组映射为对象
export const ArrToObj = (
    arr: anyObj[],
    key = 'code',
    value = 'value',
): { [key: string]: string } => {
    const obj: anyObj = {};
    let i = 0;
    for (i; i < arr.length; i++) {
        obj[arr[i][key]] = arr[i][value];
    }
    return obj;
};

// // 正则处理公众号聊天消息表情包
// export const contentAccountToEmoji = (content: string): string => {
//     const accountEmojiMap = Object.keys(account_emojiMap);
//     let replacedText = content;
//     accountEmojiMap.forEach((key: string) => {
//         const str = key;
//         const formatStr = str
//             .replace(')', '\\)')
//             .replace('(', '\\(')
//             .replace('|', '\\|')
//             .replace('$', '\\$')
//             .replace('?', '\\?')
//             .replace('*', '\\*');

//         const regex = new RegExp(`${formatStr}`, 'g');
//         replacedText = replacedText.replace(regex, (match, p1) =>
//             emojiMap.includes(match)
//                 ? `<image class="emoji" style="width: 20px; height: 20px; vertical-align: bottom" src="${
//                       import.meta.env.VITE_OSS_HOST
//                   }/emoji/${account_emojiMap[match]}.gif" alt="${account_emojiMap[match]}" />`
//                 : `${match}`,
//         );
//     });
//     return replacedText;
// };

// // 正则处理聊天消息表情包
// export const contentToEmoji = (content: string | null) => {
//     if (!content) return;
//     const regex = /\[([^\[\]]+?)\]/g;
//     let replacedText = content.replace(regex, (match, p1) => {
//         return emojiMap.includes(p1)
//             ? `<image class="emoji" style="width: 20px; height: 20px; vertical-align: bottom" src="${
//                   import.meta.env.VITE_OSS_HOST
//               }/emoji/${emojiAllMap[`${p1}`]}.gif" alt="${emojiAllMap[`${p1}`]}" />`
//             : `[${p1}]`;
//     });

//     replacedText = contentAccountToEmoji(replacedText);
//     return replacedText;
// };

// 延迟运行
export const $sleep = (time: number) => {
    return new Promise(res => {
        const timeout = setTimeout(() => {
            res(0);
            clearTimeout(timeout);
        }, time);
    });
};

export function formCheck(rules: anyObj[], data: anyObj) {
    let res = true;
    for (const i of rules) {
        if (i instanceof Array) {
            for (const j of i) {
                if (j.required && !data[j.prop]) {
                    res = false;
                    uni.showToast({
                        title: j.message,
                        icon: 'none',
                    });
                    break;
                }
                if (j.validator) {
                    j.validator(data, data[j.prop], (result: any) => {
                        if (result instanceof Error) {
                            res = false;
                            uni.showToast({
                                title: j.message,
                                icon: 'none',
                            });
                        }
                    });
                }
            }
        } else {
            if (i.required && !data[i.prop]) {
                res = false;
                uni.showToast({
                    title: i.message,
                    icon: 'none',
                });
                break;
            }
        }
    }
    return res;
}


export function getGurrentTime () {
    let yy = new Date().getFullYear();
    let mm = (new Date().getMonth() + 1) < 10 ? '0' + (new Date().getMonth() + 1) : (new Date().getMonth() + 1);
    let dd = (new Date().getDate()) < 10 ? '0' + (new Date().getDate() + 1) : (new Date().getDate() + 1) -1;
    let hh = new Date().getHours() < 10 ? '0' + new Date().getHours() : new Date().getHours();
    let mf = new Date().getMinutes() < 10 ? '0' + new Date().getMinutes() : new Date().getMinutes();
    let ss = new Date().getSeconds() < 10 ? '0' + new Date().getSeconds() : new Date().getSeconds();
    return  yy + '-' + mm + '-' + dd + ' ' + hh + ':' + mf
  };

// // uniApi转换成同步
// function general(config: anyObj, method?: string) {
//     return new Promise((resolve, reject) => {
//         if (method) {
//             (uni as any)[method]({
//                 ...config,
//                 success(res: any) {
//                     resolve(res);
//                 },
//                 fail(err: any) {
//                     reject(err);
//                 },
//             });
//         }
//     });
// }
// export const uniSync = {
//     general,
//     chooseMedia: (...args: any[]) => general(args),
// };
